package com.top.art.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.top.art.annotation.PermissionLimit;
import com.top.art.bo.ScoreBO;
import com.top.art.entity.StudioInfo;
import com.top.art.exception.ArtException;
import com.top.art.manager.ScoreManager;
import com.top.art.service.IStudioInfoService;
import com.top.art.vo.ResVO;
import com.top.art.vo.ScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;

/**
 * @author zhubg
 * @title: ScoreController
 * @projectName art-exam-manager
 * @description: TODO
 * @date 2020/10/8-15:30
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    IStudioInfoService studioInfoService;

    @Autowired
    private ScoreManager scoreManager;


    /**
     * 查询分数
     *
     * @param bo
     * @return
     */
    @PostMapping("/queryScore")
    @PermissionLimit(limit = false)
    public ResVO queryScore(@RequestBody ScoreBO bo) {
        return new ResVO<>(scoreManager.query(bo));
    }


    /**
     * 导出成绩单
     */
    @GetMapping(value = "/downloadScore")
    @PermissionLimit(limit = false)
    public void downloadScore(HttpServletResponse response, String studioCode) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName;
        String sheetName;
        if (StringUtils.isNotBlank(studioCode)) {
            StudioInfo studioInfo = studioInfoService.getOne(Wrappers.<StudioInfo>lambdaUpdate().eq(StudioInfo::getStudioCode, studioCode));
            if (studioInfo == null) {
                throw new ArtException(404, "studioCode错误");
            }
            fileName = URLEncoder.encode(studioInfo.getStudioName(), "UTF-8").replaceAll("\\+", "%20");
            sheetName = studioInfo.getStudioName();
        } else {
            fileName = URLEncoder.encode("全部考生成绩单", "UTF-8").replaceAll("\\+", "%20");
            sheetName = "全部考生成绩单";
        }
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        Set<String> includeColumnFiledNames = new HashSet<String>();
        includeColumnFiledNames.add("identificationNumber");
        includeColumnFiledNames.add("examineeName");
        includeColumnFiledNames.add("admissionTicketCode");
        includeColumnFiledNames.add("studioName");
        includeColumnFiledNames.add("sumiao");
        includeColumnFiledNames.add("secai");
        includeColumnFiledNames.add("suxie");
        includeColumnFiledNames.add("sheji");
        includeColumnFiledNames.add("totalScore");
        includeColumnFiledNames.add("sortByStudio");
        includeColumnFiledNames.add("sortByProvince");
        includeColumnFiledNames.add("examineeGender");
        EasyExcel.write(response.getOutputStream(), ScoreVO.class).includeColumnFiledNames(includeColumnFiledNames)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet(sheetName).doWrite(scoreManager.downloadScoreByStudio(studioCode));
    }


    @GetMapping(value = "/queryScoreByTicketCode")
    @PermissionLimit(limit = false)
    public ResVO queryScoreByTicketCode(String ticketCode) {
        return new ResVO(scoreManager.getScoreByTicketCode(ticketCode));
    }

}
